﻿using System;
using System.Collections.Generic;
using System.Text;
using DreamShield.Deployment.Miles.CustomWizard;
using DreamShield.Deployment.Assistants.DreamWizard;
using System.Windows.Forms;
using DreamShield.Deployment.Installers.DreamWorker;

namespace SampleCustomWizard
{
    public class VeryBasicWizard : MilesWizardProviderBase
    {
        public virtual VeryBasicWizardUI UI { get; protected set; }

        public VeryBasicWizard(DreamWizardEngine engine)
            : base(engine)
        {
            IsInProcess = false;
            UI = new VeryBasicWizardUI();
            UI.FormClosing += new FormClosingEventHandler(UI_FormClosing);

            GuiThreadContext = UI;
        }

        void UI_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsInProcess)
            {
                if (CurrentActionId == WorkerActionId.Install)
                {
                    WorkingThread.Suspend();
                    try
                    {
                        if (MessageBox.Show("Cancel ?", "Cancel ...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            Cancel();
                        }
                    }
                    finally
                    {
                        WorkingThread.Resume();
                    }
                }
                else
                {
                    /* ON NE DOIT PAS PERMETTRE L'ANNULATION DANS UN AUTRE MODE */
                }

                e.Cancel = true;
            }
        }

        public virtual bool IsInProcess { get; protected set; }

        protected override void WizardWorkerExecute_ThreadBegin(DreamWizardEngine engine, WorkerActionId actionId, IDreamWizardWorker worker)
        {
            base.WizardWorkerExecute_ThreadBegin(engine, actionId, worker);

            IsInProcess = true;

            GuiThreadContext.Invoke(new MethodInvoker(delegate
                {
                    UI.Show();
                }));
        }

        protected override void WizardWorkerExecute_ThreadEnd(DreamWizardEngine engine, WorkerActionId actionId, IDreamWizardWorker worker)
        {
            base.WizardWorkerExecute_ThreadEnd(engine, actionId, worker);

            IsInProcess = false;

            GuiThreadContext.Invoke(new MethodInvoker(delegate
            {
                UI.Close();
            }));
        }

        public override void NotifyCustomProgress(string title, string message)
        {
            NotifyCustomProgress(title, message, -1, -1);
        }

        public override void NotifyCustomProgress(string title, string message, int pos, int max)
        {
            if (GuiThreadContext.InvokeRequired)
            {
                GuiThreadContext.Invoke(new MethodInvoker(delegate
                    {
                        NotifyCustomProgress(title, message, pos, max);
                    }));
            }
            else
            {
                UI.label1.Text = title;
                UI.label2.Text = message;

                if (pos >= 0)
                {
                    try
                    {
                        UI.progressBar1.Maximum = max;
                        UI.progressBar1.Value = pos;
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}
